package com.txhy.certificatechain.ui.activity.main;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.txhy.certificatechain.R;
import com.txhy.certificatechain.ui.activity.base.BaseActivity;
import com.txhy.certificatechain.presenter.base.BasePresenter;
import com.txhy.certificatechain.ui.adapter.FragPagerAdapter;
import com.txhy.certificatechain.ui.fragment.friend.FriendsFragment;
import com.txhy.certificatechain.ui.fragment.bottomnavigation.IndexFragment;
import com.txhy.certificatechain.ui.fragment.bottomnavigation.ShopFragment;
import com.txhy.certificatechain.ui.fragment.bottomnavigation.UserFragment;
import com.txhy.certificatechain.utils.UIUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.OnClick;

import static com.txhy.certificatechain.application.App.exit;

/*
 * -----------------------------------------------------------------
 * Copyright (C) 2014-2016, by your company, All rights reserved.
 * -----------------------------------------------------------------
 * 文    件: MainActivity.java
 * 作    者: 侯建军
 * 版    权: 1.0.0
 * 描    述:  主页
 * 创建时间: 2018/9/19 16:45
 * 更新时间：2018/9/19 16:45
 * -----------------------------------------------------------------
 * 2018/9/19 16:45 : 创建 MainActivity.java (侯建军);
 * -----------------------------------------------------------------
 */
public class MainActivity extends BaseActivity implements Serializable {
    /**
     * 滑动
     */
    @BindView(R.id.viewpager)
    ViewPager viewPager;
    @BindViews({R.id.tv_one, R.id.tv_two, R.id.tv_three, R.id.tv_four})
    public List<TextView> textViewList;
    @BindViews({R.id.img_one, R.id.img_two, R.id.img_three, R.id.img_four})
    public List<ImageView> imgViewList;
    private int imgArray[] = {R.mipmap.main_tab_home_normal, R.mipmap.main_tab_video_normal, R.mipmap.main_tab_micro_normal, R.mipmap.main_tab_me_normal};
    private int imgArraySelect[] = {R.mipmap.main_tab_home_selected, R.mipmap.main_tab_video_selected, R.mipmap.main_tab_micro_selected, R.mipmap.main_tab_me_selected};
    private List<Fragment> mFragments = new ArrayList<>();

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }
    private UserFragment userFragment;
    @Override
    protected int provideContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        //底部导航
        userFragment= UserFragment.newInstance();
        mFragments.add(IndexFragment.newInstance());
        mFragments.add(ShopFragment.newInstance());
        mFragments.add(FriendsFragment.newInstance());
        mFragments.add(userFragment);
        viewPager.setAdapter(new FragPagerAdapter(getSupportFragmentManager(), mFragments));
        viewPager.setCurrentItem(0, false);
        viewPager.setOffscreenPageLimit(4);
    }

    /**
     *
     * @param view
     */
    @OnClick({R.id.ll_index, R.id.ll_shop, R.id.ll_friend, R.id.ll_me})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            //首页
            case R.id.ll_index:
                setNo(0);
                break;
            //商城
            case R.id.ll_shop:
                setNo(1);
                break;
                //好友
            case R.id.ll_friend:
                setNo(2);
                break;
                //我的
            case R.id.ll_me:
                setNo(3);
                userFragment.setLoginUser();
                break;
        }
    }

    /**
     * 设置选中颜色
     * @param num
     */
    private void setNo(int num) {
        for (int i = 0; i < textViewList.size(); i++) {
            textViewList.get(i).setTextColor(UIUtils.getColor(R.color.main_tab_normal_color));
            imgViewList.get(i).setImageResource(imgArray[i]);
        }
        textViewList.get(num).setTextColor(UIUtils.getColor(R.color.ed584c));
        imgViewList.get(num).setImageResource(imgArraySelect[num]);
        viewPager.setCurrentItem(num);
    }

    /**
     * 程序退出
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        exit();
    }
}
